<template>
  <Dialog v-model="dialogVisible" title="选择策略" width="1000">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        ref="queryFormRef"
        :inline="true"
        :model="queryParams"
        class="-mb-15px"
        label-width="75px"
      >
        <el-form-item label="策略名称" prop="strategyName">
          <ns-input
            v-model="queryParams.strategyName"
            class="!w-240px"
            clearable
            placeholder="请输入策略名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="描述" prop="strategyDesc">
          <ns-input
            v-model="queryParams.strategyDesc"
            class="!w-240px"
            clearable
            placeholder="请输入描述"
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button @click="handleQuery">
            <Icon class="mr-5px" icon="ep:search" />
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <Icon class="mr-5px" icon="ep:refresh" />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <ContentWrap>
      <el-table v-loading="loading" :data="list" row-key="id" border ref="treeTableRef">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" width="80" label="序号" align="center" />
        <el-table-column prop="strategyName" label="方案名称" align="center" />
        <el-table-column prop="strategyDesc" label="描述" align="center" />
        <el-table-column prop="createTime" label="创建时间" align="center" />
      </el-table>
      <!-- 分页 -->
      <Pagination
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.page"
        :total="total"
        @pagination="getList"
      />
    </ContentWrap>
    <template #footer>
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { strategyPage, importStrategy } from '@/api/strategyManage/strategy/index'
import { ElTable } from 'element-plus'

const { t } = useI18n() // 国际化

const dialogVisible = defineModel('visible', { type: Boolean })
const message = useMessage() // 消息弹窗
const queryFormRef = ref() // 搜索的表单
const total = ref(10) // 列表的总页数
const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据

const treeTableRef = ref<InstanceType<typeof ElTable>>()

const queryParams = reactive({
  page: 1,
  pageSize: 10,
  strategyName: '',
  strategyDesc: '',
  type: 'amp'
})
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.page = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const { count, data } = await strategyPage(toRaw(queryParams))
    list.value = data.map((item) => {
      item.select = false
      return item
    })

    total.value = count
  } finally {
    loading.value = false
  }
}
getList()
const emit = defineEmits(['success'])
/** 提交表单 */
const handleSubmit = async (e) => {
  e.preventDefault()

  const filesId = treeTableRef.value?.getSelectionRows().map((item) => item.id)

  if (filesId && filesId.length === 0) {
    message.warning('请选择策略')
    return
  }

  await importStrategy({
    strategyIds: filesId
  })
  message.success(t('common.createSuccess'))
  dialogVisible.value = false
  // 发送操作成功的事件
  emit('success')
}
</script>
